<?php
include_once 'library/Bootstrap/Bootstrap.php';
class ModuleBootstrap implements Bootstrap{
	/**
	 * List Route
	 * @var Route[]
	 */
	private $_routes;
	/**
	 * Current Route
	 * @var array
	 */
	public $moduleRoute;
	/**
	 * HttpContext here ...
	 * @var HttpContext
	 */
	public $context;

	public function __construct(){
		$this->_init();
	}

	public function _init(){}
	public function addRoute($route){
		if(!isset($this->_routes[$route->getName()])){
			$this->_routes[$route->getName()] = $route;
		}
		else{
			$msg = "Route " . $route->getName() . " exist in route list";
			Log::error("ModuleBootstrap", $msg);
			if(Configuration::DEBUG_MOD)
			throw new BoostrapException($msg);
		}
	}
	public function getRoute(){
		return $this->_routes;
	}
	/**
	 * MainBootstrap Run.
	 * @param string $url
	 */
	public function run($url = null){
		$routed = $this->routeController($this->moduleRoute);
		if (!isset($routed[Configuration::CONTROLLER]))
		$routed[Configuration::CONTROLLER] = Configuration::CONTROLLER_DEFAULT;
		$routed = $this->_auth($routed);
		$controller = ControllerFactory::instance($routed);
		$this->setParramFromRouteToContext($routed);
		$controller->setParram($routed, $this->context);
		$controller->run();
//		echo '<pre>';
//		print_r($routed);
//		print_r($controller);
//		//		print_r($this->_routes);
//		//		print_r($this->moduleRoute);
//		//		print_r($this->context);
//		echo '</pre>';
	}
	private function setParramFromRouteToContext($route){
		foreach ($route as $key=> $value) {
			if(substr($key, 0,1) != "_"){
				$this->context->addRouteRequest($key, $value);
			}
		}
		
	}

	/**
	 * Redirect to Controller ...
	 * @param array $route
	 */
	private function routeController($route){
		$route[Route::ROUTE_URL] = $this->trimBaseUrl($route[Route::ROUTE_URL]);
		if($route[Route::ROUTE_URL] != STRING_EMPTY){
			foreach ($this->_routes as $value) {
				$r_result = $value->match($route[Route::ROUTE_URL]);
				if($r_result[Route::ROUTE_ISMATCH] == Route::ROUTE_MATCH){
					$urlstr = $r_result[Route::ROUTE_URL];
					parse_str($urlstr,$output);
					$output[Configuration::MODULE] = $route[Configuration::MODULE];
					$output = $this->trimOutput($output);
					return $output;
				}
			}
			return array(
			Configuration::MODULE => $route[Configuration::MODULE],
			Configuration::CONTROLLER => Configuration::CONTROLLER_DEFAULT,
			Configuration::ACTION => Configuration::ACTION_DEFAULT,
			Configuration::VIEW => Configuration::VIEW_DEFAULT,
			Configuration::VIEW_TYPE => Configuration::VIEW_TYPE_DEFAULT
			);
		}
		else{
			if($route[Route::ROUTE_QUERY] != STRING_EMPTY){
				parse_str($route[Route::ROUTE_QUERY],$output);
				$output[Configuration::MODULE] = $route[Configuration::MODULE];
				return $output;
			}
			else{
				return array(
				Configuration::MODULE => $route[Configuration::MODULE],
				Configuration::CONTROLLER => Configuration::CONTROLLER_DEFAULT,
				Configuration::ACTION => Configuration::ACTION_DEFAULT,
				Configuration::VIEW => Configuration::VIEW_DEFAULT,
				Configuration::VIEW_TYPE => Configuration::VIEW_TYPE_DEFAULT
				);
			}
		}
	}
	private function trimOutput($route){
		$route[Configuration::CONTROLLER] = StringUltil::trimSlash($route[Configuration::CONTROLLER]);
		$route[Configuration::ACTION] = StringUltil::trimSlash($route[Configuration::ACTION]);
		$route[Configuration::VIEW] = StringUltil::trimSlash($route[Configuration::VIEW]);
		$route[Configuration::VIEW_TYPE] = StringUltil::trimSlash($route[Configuration::VIEW_TYPE]);
		return $route;
	}
	private function trimBaseUrl($url){
		if($url == SLASH || $url == "//" || $url == "///")
			return STRING_EMPTY;
		return $url;
	}
	public function _auth($routed){
		return $routed;
	}
}